package com.cnc.service.Impl;

import com.cnc.dao.CourseMapper;
import com.cnc.dao.UserMapper;
import com.cnc.pojo.Course;
import com.cnc.pojo.User;
import com.cnc.service.CourseService;
import com.cnc.utils.JsonUtil;
import com.cnc.utils.PageUtil;
import com.cnc.utils.ResultUtil;
import com.cnc.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CourseServiceImpl implements CourseService {
    @Override
    public String getCourseList(String currentPage, String pageSize, String search,String type) {
        if(type==null){
            type="";
        }
        PageUtil pu = new PageUtil(pageSize, currentPage);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        List<Course> courseList = mapper.getCourseList(pu, search,type);
        pu.setResult(courseList);
        pu.setCountRows(mapper.getCountRows(search,type));
        sqlSession.close();
        return JsonUtil.objectToJson(pu);
    }

    @Override
    public String getCourseInfo(String id) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        Course course = mapper.getCourseById(Integer.parseInt(id));
        return JsonUtil.objectToJson(course);
    }

    @Override
    public String updateImgById(String cid, String file) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        int i = mapper.UpdateImgById(Integer.parseInt(cid), file);
        sqlSession.commit();
        return file;
    }

    public String updateCourse(String data) {
        Course course = (Course) JsonUtil.JsonToObject(data, Course.class);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        int i = mapper.updateCourse(course);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    @Override
    public String addCourse(String data) {
        Course course = (Course) JsonUtil.JsonToObject(data, Course.class);
        course.setCreateTime(new Date());
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        int i = mapper.addCourse(course);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return "添加成功！";
        }else {
            return "添加失败！";
        }
    }

    @Override
    public String delAllSelect(String data) {
        ArrayList<Integer> list = (ArrayList<Integer>) JsonUtil.JsonToObject(data, Object.class);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        list.forEach(i->mapper.delCourseById(i));
        sqlSession.commit();
        return "删除成功！";
    }

    @Override
    public String getAllCourse() {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
        List<Course> allCourse = mapper.getAllCourse();
        sqlSession.close();
        return JsonUtil.objectToJson(allCourse);
    }

    @Override
    public String getList(String currentPage, String size, String search, String type) {
        return null;
    }
}
